from django.shortcuts import render
from django.shortcuts import HttpResponse
from .models import *
from django.forms.models import model_to_dict

xy_dict = {1: {'name': '教育学院', 'py': 'img/jy.png'}, 2: {'name': '文学院', 'py': 'img/w.png'},
           3: {'name': '政治与行政学院', 'py': 'img/zx.png'}, 4: {'name': '历史文化学院', 'py': 'img/ls.png'},
           5: {'name': '外国语学院', 'py': 'img/wg.png'}, 6: {'name': '商学院', 'py': 'img/s.png'},
           7: {'name': '管理学院', 'py': 'img/gl.png'}, 8: {'name': '数学与信息学院', 'py': 'img/sx.png'},
           9: {'name': '物理与空间科学学院', 'py': 'img/wl.png'}, 10: {'name': '化学化工学院', 'py': 'img/hx.png'},
           11: {'name': '生命科学学院', 'py': 'img/sk.png'}, 13: {'name': '国土资源学院', 'py': 'img/gt.png'},
           12: {'name': '计算机学院', 'py': 'img/js.png'}, 14: {'name': '学前与初等教育学院', 'py': 'img/xq.png'},
           15: {'name': '音乐学院', 'py': 'img/yy.png'}, 16: {'name': '美术学院', 'py': 'img/m.png'},
           17: {'name': '体育学院', 'py': 'img/ty.png'}, 18: {'name': '新闻传播学院', 'py': 'img/xc.png'},
           19: {'name': '法学院', 'py': 'img/f.png'}, 20: {'name': '环境科学与工程学院', 'py': 'img/hj.png'},
           21: {'name': '电子与信息学院', 'py': 'img/dx.png'}, 22: {'name': '高等职业技术学院', 'py': 'img/gd.png'}}


def index(requst):
    """
    将每个学院的信息 name:url --时间最晚  从数据库中导出来，并存入一个字典中
    """

    collegeList = []
    for i in range(1, 23):
        try:
            collegeInfo = college.objects.filter(name=i).latest('date')
            collegeList.append({"name": xy_dict.get(i)["name"], "py": xy_dict.get(i)["py"], "title": collegeInfo.title,
                                "url": collegeInfo.url})
        except(Exception):
            continue

    img = banner.objects.filter(select=1)
    return render(requst, "index.html", {"collegeList": collegeList, "img": img})


def activity(requst):
    activeList = []
    for i in range(1, 4):
        try:
            activeTemporaryList = []
            activeInfo = active.objects.filter(select=i)[:10]
            activeTemporaryList = []
            for j in activeInfo:
                activeTemporaryList.append({"title": j.title, "url": j.url, "date": j.date})
            activeList.append(activeTemporaryList)
        except(Exception):
            pass
    img = banner.objects.filter(select=1)
    return render(requst, "activity.html", {"activeList": activeList, "img": img})


def jobPage(requst):
    jobList = []
    for i in range(1, 5):
        try:
            jobTemporaryList = []
            collegeInfo = job.objects.filter(select=i)[:10]
            jobTemporaryList = []
            for j in collegeInfo:
                jobTemporaryList.append({"title": j.title, "url": j.url, "date": j.date})
            jobList.append(jobTemporaryList)
        except(Exception):
            continue
    img = banner.objects.filter(select=1)
    return render(requst, "job.html", {"jobList": jobList, "img": img})


def tinkerPage(requst):
    tinkerList = []
    for i in range(1, 4):
        try:
            tinkerTemporaryList = []
            tinkerInfo = tinker.objects.filter(select=i)[:10]
            tinkerTemporaryList = []
            for j in tinkerInfo:
                tinkerTemporaryList.append({"title": j.title, "url": j.url, "date": j.date})
            tinkerList.append(tinkerTemporaryList)
        except(Exception):
            continue
    img = banner.objects.filter(select=1)
    return render(requst, "tinker.html", {"tinkerList": tinkerList, "img": img})
